package adapter;

import java.security.spec.InvalidParameterSpecException;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.remotecontrol.R;
import components.AlarmManager;
import components.AlertDialogManager;

import data.RCDevice;

public class DeviceAdapter extends ArrayAdapter<String[]> {

	private Context context = null;
	private List<RCDevice> mListItem = null;
	private int mResourceID = 0;
	private DeviceAdapterInterface mInterface;

	public DeviceAdapter(Context context, List<RCDevice> objects,
			DeviceAdapterInterface deviceAdapterinterface) {
		super(context, R.layout.item_device);

		this.context = context;
		mListItem = objects;
		mResourceID = R.layout.item_device;
		mInterface = deviceAdapterinterface;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mListItem == null) {
			return 0;
		}
		return mListItem.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getApplicationContext().getSystemService(
							Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(this.mResourceID, null);
		} else {
			row = convertView;
		}
		final RCDevice item = mListItem.get(position);

		TextView textView = (TextView) row.findViewById(R.id.item_device_name);
		final ToggleButton toggleButton = (ToggleButton) row
				.findViewById(R.id.item_device_toggle_power);
		textView.setText(item.getName());
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(context);
				dialog.setContentView(R.layout.dialog_change_name);
				dialog.setTitle(context.getString(R.string.alert_title));

				// set the custom dialog components - text, image and button
				// TextView text = (TextView) dialog.findViewById(R.id.text);
				// text.setText("Android custom dialog example!");
				// ImageView image = (ImageView)
				// dialog.findViewById(R.id.image);
				// image.setImageResource(R.drawable.ic_launcher);
				Button cancleDialogButton = (Button) dialog.findViewById(R.id.btn_cancel);
				cancleDialogButton.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				
				Button dialogButton = (Button) dialog.findViewById(R.id.btn_OK);
				// if button is clicked, close the custom dialog
				dialogButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// CheckBox chk = (CheckBox)
						// dialog.findViewById(R.id.chk_not_show_again);
						// if (chk.isChecked()) {
						// SharePreferencesManager.dontShowPowerConfirm(context);
						// }

						EditText editText = (EditText) dialog
								.findViewById(R.id.item_edit_new_device_name);
						String newName = editText.getText().toString();

						if (newName.isEmpty()) {
							return;
						}

						try {
							AlarmManager.sharedAlarmList(getContext())
									.changeDeviceName(item, newName);
						} catch (InvalidParameterSpecException e) {
							// TODO Auto-generated catch block
							AlertDialogManager.createAlertDialog(getContext(),
									context.getString(R.string.alert_title),
									e.getMessage(),
									context.getString(R.string.OK),
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									}).show();
						}
						DeviceAdapter.this.notifyDataSetInvalidated();
						dialog.dismiss();
					}
				});
				
				dialog.show();
			}
		});
		toggleButton.setChecked(item.isOn());

		if (item.isOn()) {
			toggleButton.setBackgroundResource(R.drawable.alarm_normal_state);
		} else {
			toggleButton.setBackgroundResource(R.drawable.alarm_alert_state);
		}

		toggleButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean isChecked = toggleButton.isChecked();

				if (mInterface != null) {
					if (isChecked) {
						mInterface.clickOn(toggleButton, item);
					} else {
						mInterface.clickOff(toggleButton, item);

					}
				}

				toggleButton.setChecked(!isChecked);
			}
		});

		return row;
	}

}
